<?php
    // +----------------------------------------------------------------------
    // | 海豚PHP框架 [ DolphinPHP ]
    // +----------------------------------------------------------------------
    // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
    // +----------------------------------------------------------------------
    // | 官方网站: http://dolphinphp.com
    // +----------------------------------------------------------------------
    // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------

    namespace app\cms\api;

    use app\cms\model\Column as ColumnModel;
    use think\Db;
    use util\Tree;

    /**
     * 前台栏目文档列表控制器
     *
     * @package app\cms\admin
     */
    class Column extends Common
    {

        /**
         *
         * 栏目文章列表
         * api POST /cms/column/index
         *
         * @param  $id int  栏目id
         *
         * @return $data_list array  栏目列表
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function index($id = null)
        {

            if ($id === null)
                $this->error('缺少参数');
            $map = [
                'status' => 1,
                'id'     => $id
            ];

            $list_rows = $this->request->param('lists_rows');

            $column = Db::name('cms_column')->where($map)->find();
            if (!$column)
                $this->error('该栏目不存在');

            $model = Db::name('cms_model')->where('id',$column['model'])->find();

            $data = [];
            if ($model['type'] == 2)
            {
                $cid_all   = ColumnModel::getChildsId($id);
                $cid_all[] = (int)$id;

                $map = [
                    $model['table'] . '.trash'  => 0,
                    $model['table'] . '.status' => 1,
                    $model['table'] . '.cid'    => ['in',join(',',$cid_all)]
                ];

                $data_list     = Db::view($model['table'],true)//
                ->view('admin_user','username',$model['table'] . '.uid=admin_user.id','left')->where($map)//
                ->order('sort desc,id desc')->paginate($list_rows,false,['query' => request()->param()]);
                $data['model'] = $column['model'];
            }
            else
            {
                $cid_all   = ColumnModel::getChildsId($id);
                $cid_all[] = (int)$id;

                $map = [
                    'cms_document.trash'  => 0,
                    'cms_document.status' => 1,
                    'cms_document.cid'    => ['in',$cid_all]
                ];

                $data_list     = Db::view('cms_document',true)//
                ->view('admin_user','username','cms_document.uid=admin_user.id','left')//
                ->view($model['table'],'*','cms_document.id=' . $model['table'] . '.aid','left')//
                ->where($map)->order('sort desc,id desc')->paginate($list_rows,false,['query' => request()->param()]);
                $data['model'] = '';
            }

            $countPage          = $data_list->render() ? $data_list->render() : 1;
            $data_list          = $data_list->toArray();
            $data_list['pages'] = $countPage;
            foreach ($data_list['data'] as &$item)
            {
                if (isset($item['img']))
                {
                    $item['img'] = get_file_path($item['img']);
                }
                if (isset($item['create_time']))
                {
                    $item['create_time'] = format_time($item['create_time']);
                }
                if (isset($item['update_time']))
                {
                    $item['update_time'] = format_time($item['update_time']);
                }


            }
            unset($item);
            $this->result($data_list);
        }

        /**
         * 获取栏目详情
         * api POST /cms/column/info
         *
         * @param  $id null  栏目ID
         *
         * @return $column array 栏目详情说明
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function info($id = null)
        {

            if ($id === null)
                $this->error('缺少参数');
            $map    = [
                'status' => 1,
                'id'     => $id
            ];
            $column = Db::name('cms_column')->where($map)->find();
            if (!$column)
                $this->error('该栏目不存在');
            $this->result($column);
        }

        /**
         * 获取面包屑
         * api POST /cms/column/getBreadcrumb
         *
         * @param $id int 栏目ID
         */
        public function getBreadcrumb($id)
        {

            $columns = ColumnModel::where('status',1)->column('id,pid,name,url,target,type');
            foreach ($columns as &$column)
            {
                if ($column['type'] == 0)
                {
                    $column['url'] = url('cms/column/index',['id' => $column['id']]);
                }
            }
            return $this->result(Tree::config(['title' => 'name'])->getParents($columns,$id));
        }
    }